import numpy as np

class PEP(object):
    def __init__(self):
        self.world = [100,100]
        self.food  = [np.random.randint(0,100),np.random.randint(0,100)]
        self.pep   = [np.random.randint(0,100),np.random.randint(0,100)]
        self.step_ = 0
        self.win = 0
        self.r2_1000 = 0
        self.ac_1000 = 0
        
    def s_len(self):
        return 4
        
    def a_len(self):
        return 2
        
    def b(self):
        return 1
        
    def step(self,a):
        self.step_ += 1
        r1 = ((((self.pep[0] - self.food[0])**2 + (self.pep[1] - self.food[1])**2) ** 0.5)) / 200
        self.pep[0] += round(a[0]*1)
        self.pep[1] += round(a[1]*1)
        
        if self.pep[0] >= 100:
            self.pep[0] = 100
        if self.pep[0] <= 0:
            self.pep[0] = 0
        if self.pep[1] >= 100:
            self.pep[1] = 100
        if self.pep[1] <= 0:
            self.pep[1] = 0
            
        r2 = ((((self.pep[0] - self.food[0])**2 + (self.pep[1] - self.food[1])**2) ** 0.5)) / 200
        
        if r1 > r2:
            # r = 1
            self.win += 1
        # else:    
            # r = -1
        r = ( r1 - r2) * 100 - 0.0001 
        self.r2_1000 += r2*200
        self.ac_1000 += ((a[0]*1)**2+(a[1]*1)**2)**0.5
        
        if (self.pep == self.food):
            
            print(self.step_)
            print(self.food)
            print(self.pep)
            print()
            self.food  = [np.random.randint(0,100),np.random.randint(0,100)]
            #self.pep   = [np.random.randint(0,100),np.random.randint(0,100)]
            s = np.array(self.MAXMIN(self.food + self.pep))
            
            return s,r
        
        if self.step_ % 1000 == 0:
            print(self.step_)
            print(self.food)
            print(self.pep)
            print('self.win/self.step_',self.win/self.step_)
            print('r2_1000',self.r2_1000/1000)
            print('self.ac_1000',self.ac_1000 / 1000)
            print()
            self.r2_1000 = 0
            self.ac_1000 = 0
            # self.pep   = [np.random.randint(0,100),np.random.randint(0,100)]
            

            
        self.food  = [np.random.randint(0,100),np.random.randint(0,100)]
        s = np.array(self.MAXMIN(self.food + self.pep))
        
        if self.step_ > 1000000:
            self.step_ = 1
            self.win   = 0
        
        
        return s,r
        
    def reset(self):
        return np.array(self.MAXMIN(self.food + self.pep))
        
    def MAXMIN(self,data):
        return np.array(data)/100
        
        